#include "missioneditview.h"

#include <QDropEvent>
#include <QTreeWidget>
#include <QDebug>
#include<QMenu>
#include "taskmanager.h"
#include<QFileDialog>
const QFont TextFont = QFont(QStringLiteral("微软雅黑"), 10);


MissionEditView::MissionEditView(QWidget *parent) : HPlot(parent)
  , loopTime(25)
{
    this->setObjectName("WidgetMissionEditViewObject");
    this->setWindowTitle(tr("MissionEditView"));

    this->setInteractions((HMP::iRangeDrag |\
                           HMP::iRangeZoom |\
                           HMP::iSelectItems));



    loopTime=LoopTime;
    connect(xAxis,SIGNAL(rangeChanged(HRange)),xAxis2,SLOT(setRange(HRange)));
    connect(yAxis,SIGNAL(rangeChanged(HRange)),yAxis2,SLOT(setRange(HRange)));
    this->xAxis->setRange(-0.75, loopTime+0.75);
    this->yAxis->setRange(-loopTime, 900+loopTime);
    this->xAxis->setAutoTickCount(loopTime);
    this->yAxis->setAutoTickCount(900/loopTime);
    this->yAxis->setRangeReversed(true);
    this->yAxis2->setRangeReversed(true);

    xAxis->setTickLabelFont(TextFont);
    yAxis->setTickLabelFont(TextFont);
    xAxis2->setTickLabelFont(TextFont);
    yAxis2->setTickLabelFont(TextFont);

    xAxis->setBasePen(QPen(Qt::white, 1));
    yAxis->setBasePen(QPen(Qt::white, 1));
    xAxis->setTickPen(QPen(Qt::white, 1));
    yAxis->setTickPen(QPen(Qt::white, 1));
    xAxis->setSubTickPen(QPen(Qt::white, 1));
    yAxis->setSubTickPen(QPen(Qt::white, 1));
    xAxis->setTickLabelColor(Qt::white);
    yAxis->setTickLabelColor(Qt::white);
    xAxis2->setBasePen(QPen(Qt::white, 1));
    yAxis2->setBasePen(QPen(Qt::white, 1));
    xAxis2->setTickPen(QPen(Qt::white, 1));
    yAxis2->setTickPen(QPen(Qt::white, 1));
    xAxis2->setSubTickPen(QPen(Qt::white, 1));
    yAxis2->setSubTickPen(QPen(Qt::white, 1));
    xAxis2->setTickLabelColor(Qt::white);
    yAxis2->setTickLabelColor(Qt::white);


    xAxis->setAutoTickStep(true);
    xAxis2->setAutoTickStep(true);
    yAxis->setAutoTickStep(true);
    yAxis2->setAutoTickStep(true);

    this->axisRect()->setupFullAxesBox();

    this->xAxis->setTickLabels(true);
    this->xAxis2->setTickLabels(true);

    this->yAxis->setTickLabels(true);
    this->yAxis2->setTickLabels(false);

    this->setAutoFillBackground(true);
    setBackground(QBrush(QColor(QRgb(0x13242E))));

    this->xAxis->setLabel("");
    this->yAxis->setLabel("");

    this->xAxis->grid()->setPen(QPen(QBrush(QColor(200,200,200)),1,Qt::DotLine));
    this->yAxis->grid()->setPen(QPen(QBrush(QColor(200,200,200)),1,Qt::DotLine));

    setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Expanding);

    TaskManager::getInstance()->registNotify(this);
    bool bOK =false;
    bOK = connect(this,SIGNAL(sig_selectCommand(QString)),TaskManager::getInstance(),SLOT(slot_commandSelect(QString)));
//    qDebug()<<bOK<<"this layer count"<<this->layerCount()<<this->layer(0);


    this->addLayer("TopLayer");
}

MissionEditView::~MissionEditView()
{

}

void MissionEditView::updateTask(const QString &taskObj, const QString &cmdObj, const QString &scriptObj)
{
    if(taskObj.isEmpty())
    {
        // 清除全部item
        this->clearItems();
        QVector<TaskItem*> vctTask = TaskManager::getInstance()->allTasks();
        foreach (TaskItem* ptask, vctTask) {

            StyleItem* pStyle = TaskManager::getInstance()->getStyle(ptask->getName());

            QVector<CommandItem *> vctCommand = ptask->allCommands();
            foreach (CommandItem* pCommand, vctCommand) {
                double dSTime = pCommand->getValue(ScriptItem::Time_Start);
                double dETime = pCommand->getValue(ScriptItem::Time_End);
                int nSRow = getRow(dSTime,false);
                int nERow = getRow(dETime,true);
                if(nERow==nSRow) // 1 line
                {
                    HItemRect* pRectItem = new HItemRect(this);
                    pRectItem->setObjectName(pCommand->objectName());
                    pRectItem->setSelectable(true);

                    //if分支新增为了处理内黑体和黑体相同的名字，在图片区分内黑体
                    if((ptask->getName().split(" ").at(0)=="BlackbodyScan")&&(dETime-1e-6<dSTime))
                    {
                        pRectItem->setPen(QPen(Qt::red,2));
                        pRectItem->setLayer("TopLayer");
                    }
                    else
                    {
                         pRectItem->setPen(QPen(Qt::black,1));
                    }
                    pRectItem->setBrush(QBrush(QColor(pStyle->getBrushColor()),pStyle->getBrushStyle()));
                    pRectItem->topLeft->setCoords(dSTime-nSRow*loopTime,nSRow*loopTime-(loopTime*0.5-2));
                    pRectItem->bottomRight->setCoords(dETime-nERow*loopTime,nERow*loopTime+(loopTime*0.5-2));
                    this->addItem(pRectItem);
                    //        this->replot();
                }
                else // >= 3 line
                {
                    // 添加任务开始时间所在行
                    HItemRect* pRectItem1 = new HItemRect(this);
                    pRectItem1->setObjectName(pCommand->objectName()+"::0");
                    pRectItem1->setSelectable(true);
                    if((ptask->getName().split(" ").at(0)=="BlackbodyScan")&&(dETime-1e-6<dSTime))
                    {
                        pRectItem1->setPen(QPen(Qt::red,2));
                        pRectItem1->setLayer("TopLayer");
                    }
                    else
                    {
                        pRectItem1->setPen(QPen(Qt::black,1));
                    }
                    pRectItem1->setBrush(QBrush(QColor(pStyle->getBrushColor()),pStyle->getBrushStyle()));
                    pRectItem1->topLeft->setCoords(dSTime-nSRow*loopTime,nSRow*loopTime-(loopTime*0.5-2));
                    pRectItem1->bottomRight->setCoords(loopTime,nSRow*loopTime+(loopTime*0.5-2));
                    this->addItem(pRectItem1);

                    for(int i=nSRow+1;i<nERow;++i)
                    {
                        HItemRect* pRectItem = new HItemRect(this);
                        pRectItem->setObjectName(pCommand->objectName()+QString("::%1").arg(i-nSRow));
                        pRectItem->setSelectable(true);
                        if((ptask->getName().split(" ").at(0)=="BlackbodyScan")&&(dETime-1e-6<dSTime))
                        {
                            pRectItem->setPen(QPen(Qt::red,2));
                            pRectItem->setLayer("TopLayer");
                        }
                        else
                        {
                            pRectItem->setPen(QPen(Qt::black,1));
                        }
                        pRectItem->setBrush(QBrush(QColor(pStyle->getBrushColor()),pStyle->getBrushStyle()));
                        pRectItem->topLeft->setCoords(0,i*loopTime-(loopTime*0.5-2));
                        pRectItem->bottomRight->setCoords(loopTime,i*loopTime+(loopTime*0.5-2));
                        this->addItem(pRectItem);
                    }

                    // 添加任务结束时间所在行
                    HItemRect* pRectItem2 = new HItemRect(this);
                    pRectItem2->setObjectName(pCommand->objectName()+QString("::%1").arg(nERow-nSRow+1));
                    pRectItem2->setSelectable(true);
                    if((ptask->getName().split(" ").at(0)=="BlackbodyScan")&&(dETime-1e-6<dSTime))
                    {
                        pRectItem2->setPen(QPen(Qt::red,2));
                        pRectItem2->setLayer("TopLayer");
                    }
                    else
                        pRectItem2->setPen(QPen(Qt::black,1));
                    pRectItem2->setBrush(QBrush(QColor(pStyle->getBrushColor()),pStyle->getBrushStyle()));
                    pRectItem2->topLeft->setCoords(0,nERow*loopTime-(loopTime*0.5-2));
                    pRectItem2->bottomRight->setCoords(dETime-nERow*loopTime,nERow*loopTime+(loopTime*0.5-2));
                    this->addItem(pRectItem2);
                    //        this->replot();
                }
            }
        }
    }
    else if(cmdObj.isEmpty())
    {
        // 清除全部item
        for(int i=this->itemCount(); i>0; i--)
        {
            if(this->item(i-1)->objectName().section(":",1,1).compare(taskObj.section(":",-1,-1)) == 0)
            {
                this->removeItem(i-1);
                continue;
            }
        }

        TaskItem* pcurTask = TaskManager::getInstance()->getTask(taskObj);
        if(NULL == pcurTask)
            return;

        StyleItem* pStyle = TaskManager::getInstance()->getStyle(pcurTask->getName());

        QVector<CommandItem *> vctCommand = pcurTask->allCommands();
        foreach (CommandItem* pCommand, vctCommand) {
            double dSTime = pCommand->getValue(ScriptItem::Time_Start);
            double dETime = pCommand->getValue(ScriptItem::Time_End);
            int nSRow = getRow(dSTime,false);
            int nERow = getRow(dETime,true);
            if(nERow==nSRow) // 1 line
            {
                HItemRect* pRectItem = new HItemRect(this);
                pRectItem->setObjectName(pCommand->objectName());
                pRectItem->setSelectable(true);
                if((pcurTask->getName().split(" ").at(0)=="BlackbodyScan")&&(dETime-1e-6<dSTime))
                {
                    pRectItem->setPen(QPen(Qt::red,2));
                    pRectItem->setLayer("TopLayer");
                }
                else
                    pRectItem->setPen(QPen(Qt::black,1));
                pRectItem->setBrush(QBrush(QColor(pStyle->getBrushColor()),pStyle->getBrushStyle()));
                pRectItem->topLeft->setCoords(dSTime-nSRow*loopTime,nSRow*loopTime-(loopTime*0.5-2));
                pRectItem->bottomRight->setCoords(dETime-nERow*loopTime,nERow*loopTime+(loopTime*0.5-2));
                this->addItem(pRectItem);
                //        this->replot();
            }
            else // >= 3 line
            {
                // 添加任务开始时间所在行
                HItemRect* pRectItem1 = new HItemRect(this);
                pRectItem1->setObjectName(pCommand->objectName()+"::0");
                pRectItem1->setSelectable(true);
                if((pcurTask->getName().split(" ").at(0)=="BlackbodyScan")&&(dETime-1e-6<dSTime))
                {
                    pRectItem1->setPen(QPen(Qt::red,2));
                    pRectItem1->setLayer("TopLayer");
                }
                else
                    pRectItem1->setPen(QPen(Qt::black,1));
                pRectItem1->setBrush(QBrush(QColor(pStyle->getBrushColor()),pStyle->getBrushStyle()));
                pRectItem1->topLeft->setCoords(dSTime-nSRow*loopTime,nSRow*loopTime-(loopTime*0.5-2));
                pRectItem1->bottomRight->setCoords(loopTime,nSRow*loopTime+(loopTime*0.5-2));
                this->addItem(pRectItem1);

                for(int i=nSRow+1;i<nERow;++i)
                {
                    HItemRect* pRectItem = new HItemRect(this);
                    pRectItem->setObjectName(pCommand->objectName()+QString("::%1").arg(i-nSRow));
                    pRectItem->setSelectable(true);
                    if((pcurTask->getName().split(" ").at(0)=="BlackbodyScan")&&(dETime-1e-6<dSTime))
                    {
                        pRectItem->setPen(QPen(Qt::red,2));
                        pRectItem->setLayer("TopLayer");
                    }
                    else
                        pRectItem->setPen(QPen(Qt::black,1));
                    pRectItem->setBrush(QBrush(QColor(pStyle->getBrushColor()),pStyle->getBrushStyle()));
                    pRectItem->topLeft->setCoords(0,i*loopTime-(loopTime*0.5-2));
                    pRectItem->bottomRight->setCoords(loopTime,i*loopTime+(loopTime*0.5-2));
                    this->addItem(pRectItem);
                }

                // 添加任务结束时间所在行
                HItemRect* pRectItem2 = new HItemRect(this);
                pRectItem2->setObjectName(pCommand->objectName()+QString("::%1").arg(nERow-nSRow+1));
                pRectItem2->setSelectable(true);
                if((pcurTask->getName().split(" ").at(0)=="BlackbodyScan")&&(dETime-1e-6<dSTime))
                {
                    pRectItem2->setPen(QPen(Qt::red,2));
                    pRectItem2->setLayer("TopLayer");
                }
                else
                    pRectItem2->setPen(QPen(Qt::black,1));
                pRectItem2->setBrush(QBrush(QColor(pStyle->getBrushColor()),pStyle->getBrushStyle()));
                pRectItem2->topLeft->setCoords(0,nERow*loopTime-(loopTime*0.5-2));
                pRectItem2->bottomRight->setCoords(dETime-nERow*loopTime,nERow*loopTime+(loopTime*0.5-2));
                this->addItem(pRectItem2);
            }
        }

    }
    else
    {
        TaskItem* pcurTask = TaskManager::getInstance()->getTask(taskObj);
        if(NULL == pcurTask)
            return;

        StyleItem* pStyle = TaskManager::getInstance()->getStyle(pcurTask->getName());

        CommandItem* pCurCommand = pcurTask->getCommand(cmdObj);
        if(NULL == pCurCommand)
            return;

        for(int i=0; i<this->itemCount(); ++i)
        {
            if(this->item(i)->objectName().contains(pCurCommand->objectName()))
            {
                this->removeItem(i);
            }
        }
        double dSTime = pCurCommand->getValue(ScriptItem::Time_Start);
        double dETime = pCurCommand->getValue(ScriptItem::Time_End);
        int nSRow = getRow(dSTime,false);
        int nERow = getRow(dETime,true);
        if(nERow==nSRow) // 1 line
        {
            HItemRect* pRectItem = new HItemRect(this);
            pRectItem->setObjectName(pCurCommand->objectName());
            pRectItem->setSelectable(true);
            if((pcurTask->getName().split(" ").at(0)=="BlackbodyScan")&&(dETime-1e-6<dSTime))
            {
                pRectItem->setPen(QPen(Qt::red,2));
                pRectItem->setLayer("TopLayer");
            }
            else
                pRectItem->setPen(QPen(Qt::black,1));
            pRectItem->setBrush(QBrush(QColor(pStyle->getBrushColor()),pStyle->getBrushStyle()));
            pRectItem->topLeft->setCoords(dSTime-nSRow*loopTime,nSRow*loopTime-(loopTime*0.5-2));
            pRectItem->bottomRight->setCoords(dETime-nERow*loopTime,nERow*loopTime+(loopTime*0.5-2));
            this->addItem(pRectItem);
        }
        else // >= 3 line
        {
            // 添加任务开始时间所在行
            HItemRect* pRectItem1 = new HItemRect(this);
            pRectItem1->setObjectName(pCurCommand->objectName()+"::0");
            pRectItem1->setSelectable(true);
            pRectItem1->setPen(QPen(Qt::black,1));
            pRectItem1->setBrush(QBrush(QColor(pStyle->getBrushColor()),pStyle->getBrushStyle()));
            pRectItem1->topLeft->setCoords(dSTime-nSRow*loopTime,nSRow*loopTime-(loopTime*0.5-2));
            pRectItem1->bottomRight->setCoords(loopTime,nSRow*loopTime+(loopTime*0.5-2));
            this->addItem(pRectItem1);

            for(int i=nSRow+1;i<nERow;++i)
            {
                HItemRect* pRectItem = new HItemRect(this);
                pRectItem->setObjectName(pCurCommand->objectName()+QString("::%1").arg(i-nSRow));
                pRectItem->setSelectable(true);
                if((pcurTask->getName().split(" ").at(0)=="BlackbodyScan")&&(dETime-1e-6<dSTime))
                {
                    pRectItem->setPen(QPen(Qt::red,2));
                    pRectItem->setLayer("TopLayer");
                }
                else
                    pRectItem->setPen(QPen(Qt::black,1));
                pRectItem->setBrush(QBrush(QColor(pStyle->getBrushColor()),pStyle->getBrushStyle()));
                pRectItem->topLeft->setCoords(0,i*loopTime-(loopTime*0.5-2));
                pRectItem->bottomRight->setCoords(loopTime,i*loopTime+(loopTime*0.5-2));
                this->addItem(pRectItem);
            }

            // 添加任务结束时间所在行
            HItemRect* pRectItem2 = new HItemRect(this);
            pRectItem2->setObjectName(pCurCommand->objectName()+QString("::%1").arg(nERow-nSRow+1));
            pRectItem2->setSelectable(true);
            if((pcurTask->getName().split(" ").at(0)=="BlackbodyScan")&&(dETime-1e-6<dSTime))
            {
                pRectItem2->setPen(QPen(Qt::red,2));
                pRectItem2->setLayer("TopLayer");
            }
            else
                pRectItem2->setPen(QPen(Qt::black,1));
            pRectItem2->setBrush(QBrush(QColor(pStyle->getBrushColor()),pStyle->getBrushStyle()));
            pRectItem2->topLeft->setCoords(0,nERow*loopTime-(loopTime*0.5-2));
            pRectItem2->bottomRight->setCoords(dETime-nERow*loopTime,nERow*loopTime+(loopTime*0.5-2));
            this->addItem(pRectItem2);
        }
    }


#if 0

    int nCount = this->itemCount();
    for(int i=0; i<nCount; ++i)
    {
        HItemRect* pRectItem = dynamic_cast<HItemRect *>(this->item(i));
        if(NULL == pRectItem)
            continue;


        if(pRectItem->objectName().contains("BlackbodyScan",Qt::CaseInsensitive))
        {
            HLayer* pLayer = pRectItem->layer();

            if(qAbs(pRectItem->topLeft->key() - pRectItem->bottomRight->key()) < 1e-8)
            {


                qDebug()<<"is item null?"<<pRectItem;
//                this->removeItem(pRectItem);
                HLayer* pLayer = pRectItem->layer();
            }
        }
    }

#endif

    this->replot();

}

void MissionEditView::commandSelect(const QString &cmdObj)
{
    for(int i=0; i<itemCount(); ++i)
    {
        HItemRect* pRectItem = dynamic_cast<HItemRect*>(this->item(i));
        if(NULL == pRectItem)
            continue;

        if(pRectItem->objectName().contains(cmdObj,Qt::CaseInsensitive))
        {
            pRectItem->setSelected(true);
        }
        else
        {
            pRectItem->setSelected(false);
        }
    }

    this->replot();
}
void MissionEditView::updateStyle(const QString &sStyleName)
{
    if(sStyleName.isEmpty())
    {
        return;
    }

    StyleItem * pStyleItem = TaskManager::getInstance()->getStyle(sStyleName);
    if(NULL == pStyleItem)
        return;

    for(int i=0; i<this->itemCount(); ++i)
    {
        HItemRect* pRectItem = dynamic_cast<HItemRect*>(this->item(i));
        if(NULL == pRectItem)
            continue;

        if(pRectItem->objectName().contains(sStyleName,Qt::CaseInsensitive))
        {
            pRectItem->setBrush(QBrush(QColor(pStyleItem->getBrushColor()),pStyleItem->getBrushStyle()));
        }
        else
        {
            continue;
        }
    }

    this->replot();
}

void MissionEditView::removeTask(const QString &sTaskObj)
{
    TaskItem* pTask=TaskManager::getInstance()->getTask(sTaskObj);
    if(NULL==pTask)
        return;
    QVector<CommandItem *> vctCommand=pTask->allCommands();
    foreach(CommandItem *pCommand,vctCommand)
    {
        for(int i=this->itemCount()-1;i>=0;--i)
        {
            if(this->item(i)->objectName().contains(pCommand->objectName()))
                this->removeItem(i);
        }
    }
    this->replot();

}

void MissionEditView::mousePressEvent(QMouseEvent *event)
{
    bool temp=true;

    if(event->button() == Qt::LeftButton)
    {
        HItemRect* pRectItem = dynamic_cast<HItemRect*>(this->itemAt(event->pos()));
        if(NULL != pRectItem)
        {
            clearSelecteState();
            this->commandSelect(pRectItem->objectName().section("::",0,0));
            temp=false;
            emit sig_selectCommand(pRectItem->objectName().section("::",0,0));
        }
    }
    else if(event->button() == Qt::RightButton)
    {
        clearSelecteState();

        QMenu menu;
        menu.addAction(QIcon(),tr("SaveToPNG"),this,SLOT(slot_SaveToPNG()));
        menu.addAction(QIcon(),tr("SaveToPDF"),this,SLOT(slot_SaveToPDF()));
        menu.addAction(QIcon(),tr("ResetView"),this,SLOT(slot_ResetView()));
        menu.exec(event->globalPos());

        replot();
    }
    if(temp)
        HPlot::mousePressEvent(event);
}

void MissionEditView::clearSelecteState()
{
    int nCount = this->itemCount();
    for(int i=0; i<nCount; ++i)
    {
        HItemRect* pRectItem = dynamic_cast<HItemRect*>(this->item(i));
        if(NULL == pRectItem)
            continue;

        if(pRectItem->selected())
            pRectItem->setSelected(false);
    }
}

void MissionEditView::slot_SaveToPNG()
{
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
                                                    QDir::currentPath(),
                                                    tr("Images (*.png)"));

    if(fileName.isEmpty())
        return;

    this->savePng(fileName);
}

void MissionEditView::slot_SaveToPDF()
{
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
                                                    QDir::currentPath(),
                                                    tr("Images (*.pdf)"));

    if(fileName.isEmpty())
        return;

    this->savePdf(fileName);
}

void MissionEditView::slot_ResetView()
{
    this->xAxis->setRange(-0.75, loopTime+0.75);
    this->yAxis->setRange(-loopTime, 900+loopTime);
}

void MissionEditView::slot_addItem(QString sID)
{
#if 0
    MissionItemTree* pManager = dynamic_cast<MissionItemTree*>(sender());
    if(NULL == pManager)
        return;

    MissionItem* pMissionItem = pManager->getItem(sID);
    if(NULL == pMissionItem)
        return;

    double dSTime = pMissionItem->getStartTime();
    double dETime = pMissionItem->getEndTime();
    int nSRow = getRow(dSTime,false);
    int nERow = getRow(dETime,true);
    if(nERow==nSRow) // 1 line
    {
        HItemRect* pRectItem = new HItemRect(this);
        pRectItem->setObjectName(pMissionItem->getID());
        pRectItem->setPen(QPen(Qt::black,1));
        pRectItem->setBrush(QBrush(QColor(pMissionItem->getStyle()->getBrushColor()),pMissionItem->getStyle()->getBrushStyle()));
        pRectItem->topLeft->setCoords(dSTime-nSRow*loopTime,nSRow*loopTime-(loopTime*0.5-2));
        pRectItem->bottomRight->setCoords(dETime-nERow*loopTime,nERow*loopTime+(loopTime*0.5-2));
        this->addItem(pRectItem);
        //        this->replot();
    }
    else // >= 3 line
    {
        // 添加任务开始时间所在行
        HItemRect* pRectItem1 = new HItemRect(this);
        pRectItem1->setObjectName(pMissionItem->getID()+":0");
        pRectItem1->setPen(QPen(Qt::black,1));
        pRectItem1->setBrush(QBrush(QColor(pMissionItem->getStyle()->getBrushColor()),pMissionItem->getStyle()->getBrushStyle()));
        pRectItem1->topLeft->setCoords(dSTime-nSRow*loopTime,nSRow*loopTime-(loopTime*0.5-2));
        pRectItem1->bottomRight->setCoords(loopTime,nSRow*loopTime+(loopTime*0.5-2));
        this->addItem(pRectItem1);

        for(int i=nSRow+1;i<nERow;++i)
        {
            HItemRect* pRectItem = new HItemRect(this);
            pRectItem->setObjectName(pMissionItem->getID()+QString(":%1").arg(i-nSRow));
            pRectItem->setPen(QPen(Qt::black,1));
            pRectItem->setBrush(QBrush(QColor(pMissionItem->getStyle()->getBrushColor()),pMissionItem->getStyle()->getBrushStyle()));
            pRectItem->topLeft->setCoords(0,i*loopTime-(loopTime*0.5-2));
            pRectItem->bottomRight->setCoords(loopTime,i*loopTime+(loopTime*0.5-2));
            this->addItem(pRectItem);
        }

        // 添加任务结束时间所在行
        HItemRect* pRectItem2 = new HItemRect(this);
        pRectItem2->setObjectName(pMissionItem->getID()+QString(":%1").arg(nERow-nSRow+1));
        pRectItem2->setPen(QPen(Qt::black,1));
        pRectItem2->setBrush(QBrush(QColor(pMissionItem->getStyle()->getBrushColor()),pMissionItem->getStyle()->getBrushStyle()));
        pRectItem2->topLeft->setCoords(0,nERow*loopTime-(loopTime*0.5-2));
        pRectItem2->bottomRight->setCoords(dETime-nERow*loopTime,nERow*loopTime+(loopTime*0.5-2));
        this->addItem(pRectItem2);
        //        this->replot();
    }
#endif
}

void MissionEditView::slot_removeItem(QString sID)
{
    QStringList lstID = sID.split(";");

    foreach (QString s, lstID) {
        int itemCount = this->itemCount();
        while(itemCount--)
        {
            if(this->item(itemCount)->objectName().contains(s))
            {
                this->removeItem(itemCount);
            }
        }
    }
    this->replot();
}

/**
 * @brief MissionEditView::getRow
 * 根据任务时间，判断任务所在的行数
 * @param dTime
 * @return
 */
int MissionEditView::getRow(double dTime,bool isEndTime)
{
    int nRow = qFloor(dTime/loopTime);
    // !!! 如果是结束时间，当正好整除时，返回值应减1，该处理在函数外处理
    if(isEndTime && dTime-nRow*loopTime<1e-6)
        return nRow-1;
    else
        return nRow;
}

